layui.define(['table', 'form'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/member/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
      ,size:'lg'
    ,cols: [[
      {field: 'id', title: '会员ID'}
      ,{field: 'nickname', title: '会员昵称'}
      ,{field: 'faceImg', title: '头像',templet:function (record){
          if(record.faceImg){
              return '<img src="'+record.faceImg+'" style="height:100%;margin:auto;display:block;"/>'
          }
          return '<img src="'+layui.setter.resDomain+'/oem/member/images/202303/a8815f62434940c486fd0a4a5d5e7e9a.jpg" style="height:100%;margin:auto;display:block;"/>';
              }}
      ,{field: 'typeName',title: '会员类型'}
      ,{field: 'sexName',title: '性别'}
      ,{field: 'phone',title: '手机号'}
      ,{field: 'lastLoginDate',title: '最后登陆'}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 160, align:'center', fixed: 'right', templet:function(record){
          var html = '';
          html = html + '<a class="layui-btn layui-btn-xs" lay-event="info">查看</a>';
          html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="wallet">钱包</a>';
          html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
          return html;
      }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
      info:function(obj) {
          layer.open({
              type: 2
              , title: '查看会员详情'
              , content: layui.setter.webRoot + '/member/info.html?id=' + obj.data.id
              , maxmin: true
              , area: ['90%', '90%']
          })
      }
      ,wallet:function(obj) {
          layer.open({
              type: 2
              , title: '查看会员钱包'
              , content: layui.setter.webRoot + '/member/wallet/info.html?id=' + obj.data.id
              , maxmin: true
              , area: ['90%', '90%']
          })
      }
     ,del:function(obj){
    	layer.confirm('确定要删除该会员吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/member/del.do',
	          data:{id:obj.data.id},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
     }
  };
  
  $('.layui-btn.layui-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('member_list', {})
});